/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.hwccl.importor.users.service.persistence;

import com.hwccl.importor.users.NoSuchUsersException;
import com.hwccl.importor.users.model.Users;
import com.hwccl.importor.users.model.impl.UsersImpl;
import com.hwccl.importor.users.model.impl.UsersModelImpl;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the users service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link UsersUtil} to access the users persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Meanson Wang
 * @see UsersPersistence
 * @see UsersUtil
 * @generated
 */
public class UsersPersistenceImpl extends BasePersistenceImpl<Users>
	implements UsersPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = UsersImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_BY_SCREENNAME = new FinderPath(UsersModelImpl.ENTITY_CACHE_ENABLED,
			UsersModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findByscreenName",
			new String[] {
				String.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_COUNT_BY_SCREENNAME = new FinderPath(UsersModelImpl.ENTITY_CACHE_ENABLED,
			UsersModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countByscreenName", new String[] { String.class.getName() });
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(UsersModelImpl.ENTITY_CACHE_ENABLED,
			UsersModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(UsersModelImpl.ENTITY_CACHE_ENABLED,
			UsersModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countAll", new String[0]);

	/**
	 * Caches the users in the entity cache if it is enabled.
	 *
	 * @param users the users to cache
	 */
	public void cacheResult(Users users) {
		EntityCacheUtil.putResult(UsersModelImpl.ENTITY_CACHE_ENABLED,
			UsersImpl.class, users.getPrimaryKey(), users);
	}

	/**
	 * Caches the userses in the entity cache if it is enabled.
	 *
	 * @param userses the userses to cache
	 */
	public void cacheResult(List<Users> userses) {
		for (Users users : userses) {
			if (EntityCacheUtil.getResult(UsersModelImpl.ENTITY_CACHE_ENABLED,
						UsersImpl.class, users.getPrimaryKey(), this) == null) {
				cacheResult(users);
			}
		}
	}

	/**
	 * Clears the cache for all userses.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(UsersImpl.class.getName());
		EntityCacheUtil.clearCache(UsersImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the users.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(Users users) {
		EntityCacheUtil.removeResult(UsersModelImpl.ENTITY_CACHE_ENABLED,
			UsersImpl.class, users.getPrimaryKey());
	}

	/**
	 * Creates a new users with the primary key. Does not add the users to the database.
	 *
	 * @param id the primary key for the new users
	 * @return the new users
	 */
	public Users create(long id) {
		Users users = new UsersImpl();

		users.setNew(true);
		users.setPrimaryKey(id);

		return users;
	}

	/**
	 * Removes the users with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the users to remove
	 * @return the users that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a users with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Users remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the users with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param id the primary key of the users to remove
	 * @return the users that was removed
	 * @throws com.hwccl.importor.users.NoSuchUsersException if a users with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Users remove(long id) throws NoSuchUsersException, SystemException {
		Session session = null;

		try {
			session = openSession();

			Users users = (Users)session.get(UsersImpl.class, new Long(id));

			if (users == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
				}

				throw new NoSuchUsersException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					id);
			}

			return remove(users);
		}
		catch (NoSuchUsersException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected Users removeImpl(Users users) throws SystemException {
		users = toUnwrappedModel(users);

		Session session = null;

		try {
			session = openSession();

			if (users.isCachedModel() || BatchSessionUtil.isEnabled()) {
				Object staleObject = session.get(UsersImpl.class,
						users.getPrimaryKeyObj());

				if (staleObject != null) {
					session.evict(staleObject);
				}
			}

			session.delete(users);

			session.flush();
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(UsersModelImpl.ENTITY_CACHE_ENABLED,
			UsersImpl.class, users.getPrimaryKey());

		return users;
	}

	public Users updateImpl(com.hwccl.importor.users.model.Users users,
		boolean merge) throws SystemException {
		users = toUnwrappedModel(users);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, users, merge);

			users.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(UsersModelImpl.ENTITY_CACHE_ENABLED,
			UsersImpl.class, users.getPrimaryKey(), users);

		return users;
	}

	protected Users toUnwrappedModel(Users users) {
		if (users instanceof UsersImpl) {
			return users;
		}

		UsersImpl usersImpl = new UsersImpl();

		usersImpl.setNew(users.isNew());
		usersImpl.setPrimaryKey(users.getPrimaryKey());

		usersImpl.setId(users.getId());
		usersImpl.setCompanyId(users.getCompanyId());
		usersImpl.setScreenName(users.getScreenName());
		usersImpl.setFirstName(users.getFirstName());
		usersImpl.setLastName(users.getLastName());
		usersImpl.setMale(users.getMale());
		usersImpl.setEmail(users.getEmail());
		usersImpl.setJobtitle(users.getJobtitle());
		usersImpl.setOrganization(users.getOrganization());
		usersImpl.setDepartment(users.getDepartment());

		return usersImpl;
	}

	/**
	 * Finds the users with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the users to find
	 * @return the users
	 * @throws com.liferay.portal.NoSuchModelException if a users with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Users findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the users with the primary key or throws a {@link com.hwccl.importor.users.NoSuchUsersException} if it could not be found.
	 *
	 * @param id the primary key of the users to find
	 * @return the users
	 * @throws com.hwccl.importor.users.NoSuchUsersException if a users with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Users findByPrimaryKey(long id)
		throws NoSuchUsersException, SystemException {
		Users users = fetchByPrimaryKey(id);

		if (users == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
			}

			throw new NoSuchUsersException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				id);
		}

		return users;
	}

	/**
	 * Finds the users with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the users to find
	 * @return the users, or <code>null</code> if a users with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Users fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the users with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param id the primary key of the users to find
	 * @return the users, or <code>null</code> if a users with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Users fetchByPrimaryKey(long id) throws SystemException {
		Users users = (Users)EntityCacheUtil.getResult(UsersModelImpl.ENTITY_CACHE_ENABLED,
				UsersImpl.class, id, this);

		if (users == null) {
			Session session = null;

			try {
				session = openSession();

				users = (Users)session.get(UsersImpl.class, new Long(id));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (users != null) {
					cacheResult(users);
				}

				closeSession(session);
			}
		}

		return users;
	}

	/**
	 * Finds all the userses where screenName = &#63;.
	 *
	 * @param screenName the screen name to search with
	 * @return the matching userses
	 * @throws SystemException if a system exception occurred
	 */
	public List<Users> findByscreenName(String screenName)
		throws SystemException {
		return findByscreenName(screenName, QueryUtil.ALL_POS,
			QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the userses where screenName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param screenName the screen name to search with
	 * @param start the lower bound of the range of userses to return
	 * @param end the upper bound of the range of userses to return (not inclusive)
	 * @return the range of matching userses
	 * @throws SystemException if a system exception occurred
	 */
	public List<Users> findByscreenName(String screenName, int start, int end)
		throws SystemException {
		return findByscreenName(screenName, start, end, null);
	}

	/**
	 * Finds an ordered range of all the userses where screenName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param screenName the screen name to search with
	 * @param start the lower bound of the range of userses to return
	 * @param end the upper bound of the range of userses to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of matching userses
	 * @throws SystemException if a system exception occurred
	 */
	public List<Users> findByscreenName(String screenName, int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				screenName,
				
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<Users> list = (List<Users>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_SCREENNAME,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				StringBundler query = null;

				if (orderByComparator != null) {
					query = new StringBundler(3 +
							(orderByComparator.getOrderByFields().length * 3));
				}
				else {
					query = new StringBundler(3);
				}

				query.append(_SQL_SELECT_USERS_WHERE);

				if (screenName == null) {
					query.append(_FINDER_COLUMN_SCREENNAME_SCREENNAME_1);
				}
				else {
					if (screenName.equals(StringPool.BLANK)) {
						query.append(_FINDER_COLUMN_SCREENNAME_SCREENNAME_3);
					}
					else {
						query.append(_FINDER_COLUMN_SCREENNAME_SCREENNAME_2);
					}
				}

				if (orderByComparator != null) {
					appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
						orderByComparator);
				}

				else {
					query.append(UsersModelImpl.ORDER_BY_JPQL);
				}

				String sql = query.toString();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (screenName != null) {
					qPos.add(screenName);
				}

				list = (List<Users>)QueryUtil.list(q, getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					list = new ArrayList<Users>();
				}

				cacheResult(list);

				FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_SCREENNAME,
					finderArgs, list);

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Finds the first users in the ordered set where screenName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param screenName the screen name to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the first matching users
	 * @throws com.hwccl.importor.users.NoSuchUsersException if a matching users could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Users findByscreenName_First(String screenName,
		OrderByComparator orderByComparator)
		throws NoSuchUsersException, SystemException {
		List<Users> list = findByscreenName(screenName, 0, 1, orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("screenName=");
			msg.append(screenName);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchUsersException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the last users in the ordered set where screenName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param screenName the screen name to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the last matching users
	 * @throws com.hwccl.importor.users.NoSuchUsersException if a matching users could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Users findByscreenName_Last(String screenName,
		OrderByComparator orderByComparator)
		throws NoSuchUsersException, SystemException {
		int count = countByscreenName(screenName);

		List<Users> list = findByscreenName(screenName, count - 1, count,
				orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("screenName=");
			msg.append(screenName);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchUsersException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the userses before and after the current users in the ordered set where screenName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param id the primary key of the current users
	 * @param screenName the screen name to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the previous, current, and next users
	 * @throws com.hwccl.importor.users.NoSuchUsersException if a users with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Users[] findByscreenName_PrevAndNext(long id, String screenName,
		OrderByComparator orderByComparator)
		throws NoSuchUsersException, SystemException {
		Users users = findByPrimaryKey(id);

		Session session = null;

		try {
			session = openSession();

			Users[] array = new UsersImpl[3];

			array[0] = getByscreenName_PrevAndNext(session, users, screenName,
					orderByComparator, true);

			array[1] = users;

			array[2] = getByscreenName_PrevAndNext(session, users, screenName,
					orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected Users getByscreenName_PrevAndNext(Session session, Users users,
		String screenName, OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_USERS_WHERE);

		if (screenName == null) {
			query.append(_FINDER_COLUMN_SCREENNAME_SCREENNAME_1);
		}
		else {
			if (screenName.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_SCREENNAME_SCREENNAME_3);
			}
			else {
				query.append(_FINDER_COLUMN_SCREENNAME_SCREENNAME_2);
			}
		}

		if (orderByComparator != null) {
			String[] orderByFields = orderByComparator.getOrderByFields();

			if (orderByFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(UsersModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		if (screenName != null) {
			qPos.add(screenName);
		}

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByValues(users);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<Users> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Finds all the userses.
	 *
	 * @return the userses
	 * @throws SystemException if a system exception occurred
	 */
	public List<Users> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the userses.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of userses to return
	 * @param end the upper bound of the range of userses to return (not inclusive)
	 * @return the range of userses
	 * @throws SystemException if a system exception occurred
	 */
	public List<Users> findAll(int start, int end) throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the userses.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of userses to return
	 * @param end the upper bound of the range of userses to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of userses
	 * @throws SystemException if a system exception occurred
	 */
	public List<Users> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<Users> list = (List<Users>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				StringBundler query = null;
				String sql = null;

				if (orderByComparator != null) {
					query = new StringBundler(2 +
							(orderByComparator.getOrderByFields().length * 3));

					query.append(_SQL_SELECT_USERS);

					appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
						orderByComparator);

					sql = query.toString();
				}
				else {
					sql = _SQL_SELECT_USERS.concat(UsersModelImpl.ORDER_BY_JPQL);
				}

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<Users>)QueryUtil.list(q, getDialect(), start,
							end, false);

					Collections.sort(list);
				}
				else {
					list = (List<Users>)QueryUtil.list(q, getDialect(), start,
							end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					list = new ArrayList<Users>();
				}

				cacheResult(list);

				FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the userses where screenName = &#63; from the database.
	 *
	 * @param screenName the screen name to search with
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByscreenName(String screenName) throws SystemException {
		for (Users users : findByscreenName(screenName)) {
			remove(users);
		}
	}

	/**
	 * Removes all the userses from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (Users users : findAll()) {
			remove(users);
		}
	}

	/**
	 * Counts all the userses where screenName = &#63;.
	 *
	 * @param screenName the screen name to search with
	 * @return the number of matching userses
	 * @throws SystemException if a system exception occurred
	 */
	public int countByscreenName(String screenName) throws SystemException {
		Object[] finderArgs = new Object[] { screenName };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_SCREENNAME,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				StringBundler query = new StringBundler(2);

				query.append(_SQL_COUNT_USERS_WHERE);

				if (screenName == null) {
					query.append(_FINDER_COLUMN_SCREENNAME_SCREENNAME_1);
				}
				else {
					if (screenName.equals(StringPool.BLANK)) {
						query.append(_FINDER_COLUMN_SCREENNAME_SCREENNAME_3);
					}
					else {
						query.append(_FINDER_COLUMN_SCREENNAME_SCREENNAME_2);
					}
				}

				String sql = query.toString();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (screenName != null) {
					qPos.add(screenName);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_SCREENNAME,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Counts all the userses.
	 *
	 * @return the number of userses
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_USERS);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the users persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.hwccl.importor.users.model.Users")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<Users>> listenersList = new ArrayList<ModelListener<Users>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<Users>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	@BeanReference(type = UsersPersistence.class)
	protected UsersPersistence usersPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_USERS = "SELECT users FROM Users users";
	private static final String _SQL_SELECT_USERS_WHERE = "SELECT users FROM Users users WHERE ";
	private static final String _SQL_COUNT_USERS = "SELECT COUNT(users) FROM Users users";
	private static final String _SQL_COUNT_USERS_WHERE = "SELECT COUNT(users) FROM Users users WHERE ";
	private static final String _FINDER_COLUMN_SCREENNAME_SCREENNAME_1 = "users.screenName IS NULL";
	private static final String _FINDER_COLUMN_SCREENNAME_SCREENNAME_2 = "users.screenName = ?";
	private static final String _FINDER_COLUMN_SCREENNAME_SCREENNAME_3 = "(users.screenName IS NULL OR users.screenName = ?)";
	private static final String _ORDER_BY_ENTITY_ALIAS = "users.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Users exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Users exists with the key {";
	private static Log _log = LogFactoryUtil.getLog(UsersPersistenceImpl.class);
}